package com.avalon.core.util;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.*;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Dom4jUtils {

    public static Document load(File file) throws DocumentException {

        SAXReader saxReader = new SAXReader();

        return saxReader.read(file);
    }

    public static View loadModule(String moduleName) throws DocumentException, AvalonException {
        URL resource = Dom4jUtils.class.getResource("/module" + moduleName + "/view");
        if (ObjectUtils.isNull(resource)) return null;
        File file = new File(resource.getPath());

        if (!(file.exists() && file.isDirectory())) {
            return null;
        }

        View view = new View();

        ArrayList<File> files = FileUtils.getFiles(file);

        for (File file1 : files) {

            Document viewDoc = load(file1);

            MenuList menu = getMenu(viewDoc);

            if (ObjectUtils.isNull(view.getMenus())) {
                view.setMenus(menu);
            } else {
                view.getMenus().addAll(menu);
            }

            ViewRecordList record = getRecord(viewDoc);

            if (ObjectUtils.isNull(view.getViewRecords())) {
                view.setViewRecords(record);
            } else {
                view.getViewRecords().addAll(record);
            }

        }

        return view;
    }

    public static ViewRecordList getRecord(Document document) throws AvalonException {
        Element rootElement = document.getRootElement();

        List records = rootElement.selectNodes("record");

        Iterator iterator = records.iterator();
        ViewRecordList viewRecords = new ViewRecordList();
        while (iterator.hasNext()) {

            Element element = (Element) iterator.next();
            Attribute model = element.attribute("model");
            Attribute id = element.attribute("id");

            if (ObjectUtils.isNull(model) || ObjectUtils.isNull(id)) {
                throw new AvalonException("");
            }

            List fields = element.selectNodes("field");

            Iterator fieldIterator = fields.iterator();

            ViewRecord viewRecord = new ViewRecord();
            viewRecord.setName(id.getValue());
            viewRecord.setModel(model.getValue());

            while (fieldIterator.hasNext()) {
                Element field = (Element) fieldIterator.next();
                Attribute name = field.attribute("name");

                if (name.getValue().equals("label")) {
                    viewRecord.setLabel(field.getTextTrim());
                } else if (name.getValue().equals("service")) {
                    viewRecord.setService(field.getTextTrim());
                } else if (name.getValue().equals("arch")) {

                    List elements = field.elements();
                    if (elements.size() > 0) {
                        Element first = (Element) elements.get(0);
                        viewRecord.setMode(first.getName());
                        viewRecord.setArch(first.asXML());
                    }
                } else if (name.getValue().equals("mode")) {
                    viewRecord.setMode(field.getTextTrim());
                }
            }

            viewRecords.add(viewRecord);
        }

        return viewRecords;
    }


    public static MenuList getMenu(Document document) {
        Element rootElement = document.getRootElement();

        List menuEles = rootElement.selectNodes("menu");

        Iterator iterator = menuEles.iterator();
        MenuList menus = new MenuList();
        while (iterator.hasNext()) {

            Menu menu = new Menu();
            Element element = (Element) iterator.next();
            List attributes = element.attributes();
            Iterator iterator1 = attributes.iterator();
            while (iterator1.hasNext()) {
                Attribute attribute = (Attribute) iterator1.next();
                String name = attribute.getName();

                if (name.equals("id")) {
                    menu.setName(attribute.getValue());
                } else if (name.equals("label")) {
                    menu.setLabel(attribute.getValue());
                } else if (name.equals("service")) {
                    menu.setLabel(attribute.getValue());
                } else if (name.equals("parentId")) {
                    menu.setParentName(attribute.getValue());
                } else if (name.equals("sequence")) {
                    menu.setSequence(Integer.parseInt(attribute.getValue()));
                } else if (name.equals("action")) {
                    menu.setActionName(attribute.getValue());
                }
            }
            menus.add(menu);
        }

        return menus;
    }
}
